home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_kdelibs.idb / usr / freeware / kde / include / ktablistbox.h.z / ktablistbox.h
Encoding:
C/C++ Source or Header  |  1999-01-26  |  18.0 KB  |  524 lines

  1. /* This file is part of the KDE libraries
  2.     Copyright (C) 1997 The KDE Team
  3.  
  4.     This library is free software; you can redistribute it and/or
  5.     modify it under the terms of the GNU Library General Public
  6.     License as published by the Free Software Foundation; either
  7.     version 2 of the License, or (at your option) any later version.
  8.  
  9.     This library is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.     Library General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU Library General Public License
  15.     along with this library; see the file COPYING.LIB.  If not, write to
  16.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17.     Boston, MA 02111-1307, USA.
  18. */
  19. /* Written by Stefan Taferner <taferner@kde.org>
  20.  *            Alessandro Russo <axela@bigfoot.com>
  21.  * A multi column listbox. Requires the Qt widget set.
  22.  */
  23. #ifndef KTabListBox_h
  24. #define KTabListBox_h
  25.  
  26. #undef del_item
  27. #include <qdict.h>
  28. #include <qtablevw.h>
  29. #include <qcolor.h>
  30. #include <qpixmap.h>
  31. #include <drag.h>
  32. #include <stdlib.h>
  33.  
  34. #define MAX_SEP_CHARS 16
  35.  
  36. class KTabListBoxColumn;
  37. class KTabListBoxTable;
  38. class KTabListBoxItem;
  39. class KTabListBox;
  40. class KNumCheckButton;
  41.  
  42. typedef QDict<QPixmap> KTabListBoxDict;
  43.  
  44. //-----------------------------------------------------
  45. /**
  46. * Provides a different type of Check button.
  47. */
  48. class KNumCheckButton : public QWidget
  49. {
  50.     Q_OBJECT
  51. public:
  52.     KNumCheckButton( QWidget *_parent = 0L, const char *name = 0L );
  53.     ~KNumCheckButton() {};
  54.     void setText( const char * );
  55.      
  56. signals:
  57.     void        selected();
  58.     void        deselected();    
  59.     /** leftbutton=true if is a leftbutton, =false if is a rightbutton
  60.     *   centerbutton doubleclick events aren't emitted.
  61.     */
  62.     void    doubleclick(bool leftbutton);
  63.  
  64. protected:
  65.     virtual void leaveEvent( QEvent *_ev );
  66.     virtual void enterEvent( QEvent *_ev );
  67.     virtual void mousePressEvent( QMouseEvent * );
  68.     virtual void mouseDoubleClickEvent( QMouseEvent * );
  69.     virtual void paintEvent( QPaintEvent *event);    
  70.     
  71.  private:    
  72.     bool raised;
  73.     QString     btext;
  74. };
  75.  
  76.  
  77. //--------------------------------------------------
  78. #define KTabListBoxTableInherited QTableView
  79. class KTabListBoxTable: public QTableView
  80. {
  81.   Q_OBJECT
  82.   friend KTabListBox;
  83. public:
  84.   KTabListBoxTable(KTabListBox *owner=0);
  85.   virtual ~KTabListBoxTable();
  86.   void enableKey();
  87.   int findRealCol(int x);
  88.  
  89. protected:
  90.   virtual void focusInEvent(QFocusEvent*);
  91.   virtual void focusOutEvent(QFocusEvent*);
  92.   virtual void mouseDoubleClickEvent (QMouseEvent*);
  93.   virtual void mousePressEvent (QMouseEvent*);
  94.   virtual void mouseReleaseEvent (QMouseEvent*);
  95.   virtual void mouseMoveEvent (QMouseEvent*);
  96.   virtual void doItemSelection (QMouseEvent*, int idx);
  97.  
  98.   virtual void paintCell (QPainter*, int row, int col);
  99.   virtual int cellWidth (int col);
  100.  
  101.   void reconnectSBSignals (void);
  102.  
  103.   QPoint dragStartPos;
  104.   int dragCol, dragRow;
  105.   int selIdx;
  106.   bool dragging;
  107. };
  108.  
  109.  
  110. //--------------------------------------------------
  111. #define KTabListBoxInherited KDNDWidget
  112.  
  113. /** A multi column listbox
  114.  * Features:
  115.  *  - User resizeable columns.
  116.  *  - The order of columns can be changed with drag&drop. (Alex)
  117.  *  - 3 modes: Standard, SimpleOrder, ComplexOrder. (Alex)
  118.  * ToDo: 
  119.  *  - Configurable vertical column divisor lines. 
  120.  *  - Save all setting to config file.
  121.  *  - fix flickering into column headers.
  122.  */
  123. class KTabListBox : public KDNDWidget
  124. {
  125.   Q_OBJECT
  126.   friend KTabListBoxTable;
  127.   friend KTabListBoxColumn;
  128.  
  129. public:
  130.   enum ColumnType { TextColumn, PixmapColumn, MixedColumn };
  131.   enum OrderMode { Ascending, Descending };
  132.   enum OrderType { NoOrder, SimpleOrder, ComplexOrder };
  133.  
  134.   KTabListBox (QWidget *parent=0, const char *name=0, 
  135.            int columns=1, WFlags f=0);
  136.   virtual ~KTabListBox();
  137.  
  138.   /** This enable the key-bindings (and set StrongFocus!)
  139.    * if you don't want StrongFocus you can implement your own keyPressEvent
  140.    * and send an event to KTabListBox from there... */
  141.   void enableKey(void) { lbox.enableKey(); }
  142.   
  143.   /** Returns the number of rows */
  144.   uint count (void) const { return numRows(); };
  145.   
  146.   /** Insert a line before given index, using the separator character to separate the fields. If no index is given the line is appended at the end. Returns index of inserted item. */
  147.   virtual void insertItem (const char* string, int itemIndex=-1);
  148.  
  149.   /** Append a QStrList */
  150.   void appendStrList( QStrList const *strLst );
  151.  
  152.   /** Same as insertItem, but always appends the new item. */
  153.   void appendItem (const char* string) { insertItem(string); }
  154.  
  155.   /** Change contents of a line using the separator character to separate the fields. */
  156.   virtual void changeItem (const char* string, int itemIndex);
  157.  
  158.   /** Change part of the contents of a line. */
  159.   virtual void changeItemPart (const char* string, int itemIndex, int column);
  160.  
  161.   /** Change color of line. Changes last inserted item when itemIndex==-1 */
  162.   virtual void changeItemColor (const QColor& color, int itemIndex=-1);
  163.  
  164.   /** Get number of pixels one tab character stands for. Default: 10 */
  165.   int tabWidth(void) const { return tabPixels; }
  166.   /** Set number of pixels one tab character stands for. Default: 10 */
  167.   virtual void setTabWidth(int);
  168.  
  169.   /** Returns contents of given row/column. If col is not set the
  170.    contents of the whole row is returned, seperated with the current 
  171.    seperation character. In this case the string returned is a 
  172.    temporary string that will change on the next text() call on any
  173.    KTabListBox object. */
  174.   const QString& text(int idx, int col=-1) const;
  175.  
  176.   /** Remove one item from the list. */
  177.   virtual void removeItem (int itemIndex);
  178.  
  179.   /** Remove contents of listbox */
  180.   virtual void clear (void);
  181.  
  182.   /** Return index of current item */
  183.   int currentItem (void) const { return current; }
  184.  
  185.   /** Set the current (selected) column. colId is the value that
  186.     is transfered with the selected() signal that is emited. */
  187.   virtual void setCurrentItem (int idx, int colId=-1);
  188.  
  189.   /** Unmark all items */
  190.   virtual void unmarkAll (void);
  191.  
  192.   /** Mark/unmark item with index idx. */
  193.   virtual void markItem (int idx, int colId=-1);
  194.   virtual void unmarkItem (int idx);
  195.  
  196.   /** Returns TRUE if item with given index is marked. */
  197.   virtual bool isMarked (int idx) const;
  198.  
  199.   /** Find item at given screen y position. */
  200.   int findItem (int yPos) const { return (itemShowList[lbox.findRow(yPos)]); }
  201.  
  202.   /** Returns first item that is currently displayed in the widget. */
  203.   int topItem (void) const { return (itemShowList[lbox.topCell()]); }
  204.  
  205.   /** Change first displayed item by repositioning the visible part
  206.     of the list. */
  207.   void setTopItem (int idx) { lbox.setTopCell(itemPosList(idx)); }
  208.  
  209.   /** Set number of columns. Warning: this *deletes* the contents
  210.     of the listbox. */
  211.   virtual void setNumCols (int);
  212.  
  213.   /** Set number of rows in the listbox. The contents stays as it is. */
  214.   virtual void setNumRows (int);
  215.  
  216.   /** See the docs for the QTableView class. */
  217.   int numRows (void) const { return lbox.numRows(); }
  218.   /** See the docs for the QTableView class. */
  219.   int numCols (void) const { return lbox.numCols(); }
  220.   /** See the docs for the QTableView class. */
  221.   int cellWidth (int col) { return lbox.cellWidth(col); }
  222.   /** See the docs for the QTableView class. */
  223.   int totalWidth (void) { return lbox.totalWidth(); }
  224.   /** See the docs for the QTableView class. */
  225.   int cellHeight (int row) { return lbox.cellHeight(row); }
  226.   /** See the docs for the QTableView class. */
  227.   int totalHeight (void) { return lbox.totalHeight(); }
  228.   /** See the docs for the QTableView class. */
  229.   int topCell (void) const { return itemShowList[lbox.topCell()]; }
  230.   /** See the docs for the QTableView class. */
  231.   int leftCell (void) const { return colShowList[lbox.leftCell()]; }
  232.   /** See the docs for the QTableView class. */
  233.   int lastColVisible (void) const { return colShowList[lbox.lastColVisible()]; }
  234.   /** See the docs for the QTableView class. */
  235.   int lastRowVisible (void) const { return itemShowList[lbox.lastRowVisible()]; }
  236.   /** See the docs for the QTableView class. */
  237.   bool autoUpdate (void) const { return lbox.autoUpdate(); }
  238.   /** See the docs for the QTableView class. */
  239.   void setAutoUpdate (bool upd) { lbox.setAutoUpdate(upd); }
  240.   /** See the docs for the QTableView class. */
  241.   void clearTableFlags(uint f=~0) { lbox.clearTableFlags(f); }
  242.   /** See the docs for the QTableView class. */
  243.   uint tableFlags(void) { return lbox.tableFlags(); }
  244.   /** See the docs for the QTableView class. */
  245.   bool testTableFlags(uint f) { return lbox.testTableFlags(f); }
  246.   /** See the docs for the QTableView class. */
  247.   void setTableFlags(uint f) { lbox.setTableFlags(f); }
  248.   /** See the docs for the QTableView class. */
  249.   int findCol(int x) { return lbox.findRealCol(x); }
  250.   /** See the docs for the QTableView class. */
  251.   int findRow(int y) { return itemShowList[lbox.findRow(y)]; }
  252.   /** See the docs for the QTableView class. */
  253.   bool colXPos(int col, int* x) { return lbox.colXPos(colPosList(col),x); }
  254.   /** See the docs for the QTableView class. */
  255.   bool rowYPos(int row, int* y) { return lbox.rowYPos(itemPosList(row),y); }
  256.  
  257.   /** This call the 'compar' functions if they were been defined in 
  258.   * setColumn or else use strcmp. (i.e. if you want a case-insensitive sort
  259.   * put strcasecmp in setColumn call).
  260.   * That compar function must take as arguments two char *, and must return
  261.   * an integer less  than, equal  to,  or  greater than zero if the first
  262.   * argument is considered to be respectively  less  than,  equal  to, 
  263.   * or greater than the second. */
  264.   virtual void reorderRows();
  265.   
  266.   /** Set column caption, width, type,order-type and order-mode */
  267.   virtual void setColumn (int col, const char* caption, 
  268.               int width=0, ColumnType type=TextColumn,
  269.               OrderType ordt=NoOrder,
  270.               OrderMode omode=Descending,
  271.               bool verticalLine=false,
  272.               int (*compar)(const char *, const char *)=0L);
  273.  
  274.   /** Set column width. */
  275.   virtual void setColumnWidth (int col, int width=0);
  276.   /** Get column width. */
  277.   int columnWidth (int col) { return lbox.cellWidth(col); }
  278.  
  279.   /** Set default width of all columns. */
  280.   virtual void setDefaultColumnWidth(int width0, ...);
  281.  
  282.   /** change the Ascending/Descending mode of column col.*/
  283.   void changeMode(int col);
  284.   /** Clear all number-check-buttons (ComplexOrder only) */
  285.   void clearAllNum();
  286.   
  287.   /** Set separator character, e.g. '\t'. */
  288.   virtual void setSeparator (char sep) { sepChar = sep; } 
  289.  
  290.   /** Return separator character. */
  291.   virtual char separator (void) const { return sepChar; }
  292.  
  293.   /** For convenient access to the dictionary of pictures that this listbox understands. */
  294.   KTabListBoxDict& dict (void) { return pixDict; }
  295.  
  296.   void repaint (void);
  297.  
  298.   /** Indicates that a drag has started with given item. Returns TRUE if we are dragging, FALSE if drag-start failed. */
  299.   bool startDrag(int col, int row, const QPoint& mousePos);
  300.  
  301.   QPixmap& dndPixmap(void) { return dndDefaultPixmap; }
  302.  
  303.   /** Read the config file entries in the group with the name of the listbox and set the default column widths and those. */
  304.   virtual void readConfig(void);
  305.  
  306.   /** Write the config file entries in the group with the name of the listbox*/
  307.   virtual void writeConfig(void);
  308.   
  309.   /** Return the actual position of the colum in the table.*/
  310.   int colPosList(int num);
  311.   
  312.   /** Return the actual positon of the row number num.*/
  313.   int itemPosList(int num);
  314.  
  315.   /** Get/set font of the table. font() and setFont() apply to the
  316.     caption only. */
  317.   const QFont& tableFont(void) const { return lbox.font(); }
  318.   void setTableFont(const QFont& fnt) { lbox.setFont(fnt); }
  319.   
  320. signals:
  321.   /** emited when the current item changes (either via setCurrentItem() or via mouse single-click). */
  322.   void highlighted (int Index, int column);
  323.  
  324.   /** emitted when the user double-clicks into a line. */
  325.   void selected (int Index, int column);
  326.  
  327.   /** emitted when the user presses the right mouse button over a line. */
  328.   void popupMenu (int Index, int column);
  329.  
  330.   /** emitted when the user presses the middle mouse button over a line. */
  331.   void midClick (int Index, int column);
  332.  
  333.   /** emitted when the user clicks on a column header. */
  334.   void headerClicked (int column);
  335.  
  336. protected slots:
  337.   void horSbValue(int val);
  338.   void horSbSlidingDone();
  339.  
  340. protected:
  341.   /** Used to create new column objects. Overwrite this method
  342.    * in a subclass to have your own column objects (e.g. with custom
  343.    * data in it). You will then also need customData()/setCustomData()
  344.    * methods in here that access the elememts in itemList[]. */
  345.   virtual KTabListBoxColumn* newKTabListBoxColumn(void);
  346.  
  347.   bool itemVisible (int idx) { return lbox.rowIsVisible(idx); }
  348.   void updateItem (int idx, bool clear = TRUE);
  349.   bool needsUpdate (int id);
  350.  
  351.   /// Internal method called by keyPressEvent.
  352.   void setCItem  (int idx);
  353.   /// Adjust the number in the number check boxes.
  354.   void adjustNumber(int num);
  355.   // This should really go into kdecore and should be used by all
  356.   // apps that have long scrollable widgets.
  357.   void flushKeys();
  358.   
  359.   /// For internal use.
  360.   bool recursiveSort(int level,int n,KTabListBoxColumn **c,int *iCol);
  361.   
  362.   KTabListBoxItem* getItem (int idx);
  363.   const KTabListBoxItem* getItem (int idx) const;
  364.  
  365.   virtual void keyPressEvent(QKeyEvent*);
  366.   virtual void resizeEvent (QResizeEvent*);
  367.   virtual void paintEvent (QPaintEvent*);
  368.   virtual void mouseMoveEvent(QMouseEvent*);
  369.   virtual void mousePressEvent(QMouseEvent*);
  370.   virtual void mouseReleaseEvent(QMouseEvent*);
  371.  
  372.   /** Resize item array. Per default enlarge it to double size. */
  373.   virtual void resizeList (int newNumItems=-1);
  374.  
  375.   /** Called to set drag data, size, and type. If this method returns FALSE then no drag occurs. */
  376.   virtual bool prepareForDrag (int col, int row, char** data, int* size, 
  377.                    int* type);
  378.  
  379.   /** Internal method that handles resizing of columns with the mouse. */
  380.   virtual void doMouseResizeCol(QMouseEvent*);
  381.  
  382.   /** Internal method that handles moving of columns with the mouse. */
  383.   virtual void doMouseMoveCol(QMouseEvent*);
  384.   
  385.   // This array contain the list of columns as they are inserted.
  386.   KTabListBoxColumn**    colList;
  387.   // This array contain the column numbers as they are shown.
  388.   int *colShowList;
  389.   // This array contain the row numbers as they are shown.
  390.   int *itemShowList;
  391.   KTabListBoxItem**    itemList;
  392.   int            maxItems, numColumns;
  393.   int            current;
  394.   char            sepChar;
  395.   KTabListBoxDict    pixDict;
  396.   KTabListBoxTable    lbox;
  397.   int            labelHeight;
  398.   
  399.   QPixmap        dndDefaultPixmap;
  400.   QPixmap        upPix,downPix;
  401.   QPixmap        disabledUpPix,disabledDownPix;
  402.   int            columnPadding;
  403.   QColor        highlightColor;
  404.   int            tabPixels;
  405.   bool            mResizeCol;
  406.   bool            stopOrdering;
  407.   bool            needsSort;
  408.   /// contain the number of the last column where the user clicked on checkbutton.
  409.   int                   lastSelectedColumn;
  410.   int            nMarked; //number of marked rows.
  411.   int            mSortCol;  // selected column for sorting order or -1
  412.  
  413.   int        mMouseCol; // column where the mouse action started
  414.                // (resize, click, reorder)
  415.   int        mMouseColLeft; // left offset of mouse column
  416.   int        mLastX; // Used for drawing the XORed rect in column-drag.
  417.   int        mMouseColWidth; // width of mouse column
  418.   QPoint        mMouseStart;
  419.   bool        mMouseAction;
  420.   bool        mMouseDragColumn; // true when dragging the header of a column.
  421.  
  422. private:
  423.   // Disabled copy constructor and operator=
  424.   KTabListBox (const KTabListBox &) {}
  425.   KTabListBox& operator= (const KTabListBox&) { return *this; }
  426. };
  427.  
  428.  
  429. //--------------------------------------------------
  430. class KTabListBoxItem
  431. {
  432. public:
  433.   KTabListBoxItem(int numColumns=1);
  434.   virtual ~KTabListBoxItem();
  435.  
  436.   virtual const QString& text(int column) const { return txt[column]; }
  437.   void setText (int column, const char *text) { txt[column] = text; }
  438.   virtual void setForeground (const QColor& fg ) { fgColor = fg; }
  439.   const QColor& foreground (void) { return fgColor; }
  440.  
  441.   KTabListBoxItem& operator= (const KTabListBoxItem&);
  442.  
  443.   int marked (void) const { return mark; }
  444.   bool isMarked (void) const { return (mark >= -1); }
  445.   virtual void setMarked (int m) { mark = m; }
  446.  
  447. private:
  448.   QString* txt;
  449.   int columns;
  450.   QColor fgColor;
  451.   int mark;
  452.  
  453.   friend class KTabListBox;
  454. };
  455.  
  456. typedef KTabListBoxItem* KTabListBoxItemPtr;
  457.  
  458.  
  459. //--------------------------------------------------
  460. class KTabListBoxColumn: public QObject
  461. {
  462.   Q_OBJECT
  463.  
  464. public:
  465.   KTabListBoxColumn (KTabListBox* parent, int w=0);
  466.   virtual ~KTabListBoxColumn();
  467.  
  468.   int width (void) const { return iwidth; }
  469.   virtual void setWidth (int w) { iwidth = w; }
  470.  
  471.   int defaultWidth (void) const { return idefwidth; }
  472.   virtual void setDefaultWidth (int w) { idefwidth = w; }
  473.  
  474.   virtual void setType (KTabListBox::ColumnType lbt) { colType = lbt; }
  475.   KTabListBox::ColumnType type (void) const { return colType; }
  476.  
  477.   /// @return true if need repaint.
  478.   virtual bool changeMode (void);
  479.   
  480.   virtual void setNumber(int num);
  481.   int number (void) const { return inumber;}
  482.   void hideCheckButton(void) { if(mbut) mbut->hide(); }
  483.   virtual void setOrder (KTabListBox::OrderType type,
  484.                              KTabListBox::OrderMode mode);
  485.   KTabListBox::OrderType orderType (void) const {return ordtype;}
  486.   KTabListBox::OrderMode orderMode (void) const {return ordmode;}
  487.   
  488.   virtual void paintCell (QPainter*, int row, const QString& string, 
  489.               bool marked);
  490.   virtual void paint (QPainter*);
  491.  
  492. protected slots:
  493.   virtual void setButton();
  494.   virtual void resetButton();
  495.   virtual void clearAll(bool leftbutton);
  496.   
  497. protected:
  498.   int iwidth, idefwidth, inumber;
  499.   KTabListBox::OrderMode ordmode;
  500.   KTabListBox::OrderType ordtype;
  501.   KTabListBox::ColumnType colType;
  502.   KTabListBox* parent;
  503.   KNumCheckButton *mbut;
  504. public:
  505.   int (*columnSort)(const char *, const char *);
  506.   bool vline; // if true print a vertical line to the end of column.
  507. };
  508.  
  509. typedef KTabListBoxColumn* KTabListBoxColumnPtr;
  510.  
  511.  
  512. inline KTabListBoxItem* KTabListBox :: getItem (int idx)
  513. {
  514.     return ((idx>=0 && idx<maxItems) ? itemList[idx] : (KTabListBoxItem*)0L);
  515. }
  516.  
  517. inline const KTabListBoxItem* KTabListBox :: getItem (int idx) const
  518. {
  519.   return ((idx>=0 && idx<maxItems) ? itemList[idx] : (KTabListBoxItem*)0L);
  520. }
  521.  
  522. #endif /*KTabListBox_h*/
  523.